package com.alcatellucent.ldap;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.alcatellucent.R;
import com.alcatellucent.app.LDAPApp;
import com.alcatellucent.ldap.service.LdapMCSCustomerService;
import com.alcatellucent.ldap.util.LdapMCSCustomerAdapter;
import com.alcatellucent.vo.MCSCustomerVO;
import com.alcatellucent.vo.UserVO;
import com.unboundid.ldap.sdk.LDAPConnection;

public class MCSActivity extends ExpandableListActivity {
	
	private ExpandableListView mcsCustomerView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mcslayout);
		getMCSCustomerList();
	}
	
	private void getMCSCustomerList() {

		LdapMCSCustomerService service = new LdapMCSCustomerService();
		LDAPConnection connection = ((LDAPApp) getApplicationContext())
				.getConnection();
		if (connection != null) {
			final ArrayList<MCSCustomerVO> mcsCustomerList = service
					.getMCSCustomerList(connection);
			LdapMCSCustomerAdapter mcsCustomerAdapter = null;
			if (mcsCustomerList != null) {
				//departmentView = (ExpandableListView) findViewById(R.id.expandableListView1);
				mcsCustomerView = getExpandableListView();
				mcsCustomerAdapter = new LdapMCSCustomerAdapter(
						getApplicationContext(), mcsCustomerList);
				mcsCustomerView.setAdapter(mcsCustomerAdapter);
				
				mcsCustomerView.setOnChildClickListener(new OnChildClickListener() {
					
					public boolean onChildClick(ExpandableListView parent, View v,
							int groupPosition, int childPosition, long id) {
						
						Intent userInfoIntent = new Intent(getApplicationContext(),UserDetailedInfoActivty.class);
						
						UserVO userVO = mcsCustomerList.get(groupPosition).getUserList().get(childPosition);
						
						userInfoIntent.putExtra("userdetailinfo", userVO);
						
						startActivity(userInfoIntent);
						return false;
					}
				});
			}
		}
	}


}
